﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Diagnostics;
using System.Net.Http;
using CommHelper;
using System.Web;

namespace SocketSvrBiz
{
    public delegate void SendResponse(byte[] response);

    public class AccessInterface
    {
        private Type clsType = null;
        private object clsObj = null;

        public AccessInterface()
        {
        }

        /// <summary>
        /// 创建接口实例
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        private object CreateClassInstance(string assemblyName, string typeName)
        {
            try
            {
                if ((null == clsObj) || (null == clsType))
                {
                    Assembly asm = null;
                    if (null == assemblyName)
                    {
                        asm = Assembly.GetExecutingAssembly();
                    }
                    else
                    {
                        asm = Assembly.Load(assemblyName);
                    }

                    clsType = Type.GetType(typeName);

                    clsObj = Activator.CreateInstance(clsType);
                }
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(e.Message);
            }

            return clsObj;
        }

        /// <summary>
        /// 调用指定方法
        /// </summary>
        /// <param name="mi"></param>
        /// <param name="strJson"></param>
        private void InvokeMethod(MethodInfo mi, string strJson)
        {
            try
            {
                mi.Invoke(clsObj, new object[] { strJson });
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(e.Message);
            }
        }

        /// <summary>
        /// 调用指定方法
        /// </summary>
        /// <param name="mi"></param>
        /// <param name="encJson"></param>
        private void InvokeMethod(MethodInfo mi, byte[] encJson)
        {
            try
            {
                mi.Invoke(clsObj, new object[] { encJson });
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(e.Message);
            }
        }

        /// <summary>
        /// 分派函数调用
        /// </summary>
        /// <param name="method"></param>
        /// <param name="strJson"></param>
        public void Dispatch(string method, string strJson, UserToken userToken)
        {
            try
            {
                MethodInfo mi = HttpRuntime.Cache[method] as MethodInfo;
                if ((null == mi) || (null == clsObj) || (null == clsType))
                {
                    (CreateClassInstance(null, "SocketSvrBiz.BaseBiz") as BaseBiz).userToken = userToken;

                    Type[] types = new Type[1];
                    mi = clsType.GetMethod(method,
                                           BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                           null,
                                           new Type[] { typeof(string) },
                                           null);
                    HttpRuntime.Cache.Insert(method, mi);
                }

                InvokeMethod(mi, strJson);
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(e.Message);
            }
        }

        /// <summary>
        /// 分派函数调用
        /// </summary>
        /// <param name="method"></param>
        /// <param name="encJson"></param>
        public void Dispatch(string method, byte[] encJson, UserToken userToken)
        {
            try
            {
                MethodInfo mi = HttpRuntime.Cache[method] as MethodInfo;
                if ((null == mi) || (null == clsObj) || (null == clsType))
                {
                    (CreateClassInstance(null, "SocketSvrBiz.BaseBiz") as BaseBiz).userToken = userToken;

                    Type[] types = new Type[1];
                    mi = clsType.GetMethod(method,
                                           BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public,
                                           null,
                                           new Type[] { typeof(byte[]) },
                                           null);
                    HttpRuntime.Cache.Insert(method, mi);
                }

                InvokeMethod(mi, encJson);
            }
            catch (Exception e)
            {
                LogHelper.WriteErrorLog(e.Message);
            }
        }
    }
}